<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <245369209@qq.com> 
// +----------------------------------------------------------------------

namespace app\admin\controller\api;
use app\common\controller\Api;
use think\response\Json;

/**
 * 日志管理
 * @package app\logs
 * @author LJQ<245369209@qq.com>
 */
class Logs extends Api

{

    /**
   *  日志列表
   * @param string 
   * @return Json
   */

  public function logsList(){
       $with='user';
    //分页参数
    if(($limit = input('limit')) && ($page = input('page'))){
      $startLimit =  ($page-1)*$limit;
      // 日志列表
      $info =collection(model('common/logs')->with($with)->limit($startLimit.','.$limit)->select())->toArray();
    }else{
      //日志列表
      $info =collection(model('common/logs')->with($with)->select())->toArray();
    }
       
    //总数
       $count = model('common/logs')->count('id'); 
    
              if ($info && $count == 0) {
              return json(['code'=>1,'msg'=>'没有找到日志','count'=>$count,'data'=>$info]);
          } 
        
          if ($count > 0){
            return json(['code'=>1,'msg'=>'日志','count'=>$count,'data'=>$info]);
          }
                return json(['code'=>0,'msg'=>'数据异常']);
       }
      
          //添加日志
     public function add($data){
          
          if (empty($data)) {
              return json(['code'=>0,'msg'=>'数据异常,请检查']);
          }

          $model = model('common/logs');
       
          $result = $model->change($data);
          if (false != $result) {
              return json(['code'=>1,'msg'=>'日志添加成功']);
          } else {
              return json(['code'=>0,'msg'=>'日志添加失败,请检查']);
          }
      }

    /**
   *  清空三个月前的日志
   * @param string logsId 日志id,
   * @param Json
   */
     
    public function del() {
        
        $model = model('common/logs');

        $threemonth=strtotime("-3 month");

        if($model->whereTime('createTime','<=',$threemonth)->delete() !== FALSE){
            setlog('logsdel','logs',session('user_auth.id'));
           return json(['code'=>1,'msg'=>'日志删除成功']);
       }else{
           return $this->json(['code'=>0,'msg'=>'日志删除失败,请检查']);
       }
    }

     
   /**
   *   查询日志
   * @param operatorId记录人Id
   *@param $operateType操作类型
   * @return Json
   */
    public function search($start=null,$end=null,$operatorId=null,$operateType=null){
          
          $where= [] ;
          $with='user';
           if($start&&$end){
                 $start=strtotime($start);
                 $end=strtotime($end);
                 $where['createTime'] = ['between time',[$start,$end]];
            }
            if ($operatorId) {
             $where['operatorId'] = ['eq',$operatorId];
            }

            if ($operateType) {
             $where['operateType'] = ['eq',$operateType];
            }


      //分页参数
    if(($limit = input('limit')) && ($page = input('page'))){
      $startLimit =  ($page-1)*$limit;
      //日志列表
    $info =collection(model('common/logs')->with($with)->where($where)->limit($startLimit.','.$limit)->select())->toArray();   
    }else{
      //日志列表
      $info =collection(model('common/logs')->with($with)->where($where)->select())->toArray();
    }
    //总数
         $count = model('common/logs')->where($where)->count('id'); 
        
     if (empty($info) && $count == 0) {
                return json(['code'=>0,'msg'=>'没有找到日志','count'=>$count]);
        } 
        
        if ($count > 0){
          return json(['code'=>1,'msg'=>'日志','count'=>$count,'data'=>$info]);
        }
      } 


      
}